package com.bytedance.article.common.network;

import android.net.Uri;
import android.util.Pair;

import com.bytedance.common.utility.CommonHttpException;
import com.bytedance.common.utility.e;
import com.bytedance.frameworks.baselib.network.http.exception.HttpResponseException;
import com.bytedance.retrofit2.client.Header;
import com.bytedance.retrofit2.mime.TypedByteArray;
import com.bytedance.ttnet.utils.RetrofitUtils;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public final class a extends e {
    public a() {

    }

    private static List<Header> a(Map<String, String> arg5) {
        ArrayList v0_2;
        if (arg5 == null) {
            return null;
        } else {
            ArrayList v2 = new ArrayList();
            Iterator v3 = arg5.entrySet().iterator();
            while (v3.hasNext()) {
                Map.Entry<String, String> v0_1 = (Map.Entry<String, String>) v3.next();
                v2.add(new Header(v0_1.getKey(), v0_1.getValue()));
            }

            v0_2 = v2;
        }

        return v0_2;
    }

    private static Pair<String, String> b(String arg5) throws Exception {
        Uri v1 = Uri.parse(arg5);
        StringBuilder v0 = new StringBuilder();
        String v2 = v1.getScheme();
        String v3 = v1.getHost();
        int v4 = v1.getPort();
        if (v3 != null) {
            if (v2 != null) {
                v0.append(v2);
                v0.append("://");
            }

            v0.append(v3);
            if (v4 <= 0) {
//                goto label_16;
            } else {
                v0.append(':');
                v0.append(v4);
            }
        }

//        label_16:
        v2 = v0.toString();
        String v0_1 = v1.getPath();
        String v1_1 = v1.getQuery();
        if (v1_1 != null) {
            v0_1 = v0_1 + "?" + v1_1;
        }

        return new Pair(v2, v0_1);
    }

    public final String a(String arg9, List arg10, Map arg11, com.bytedance.common.utility.e.a arg12) throws CommonHttpException {
        boolean v5_1;
        LinkedHashMap<String, String> v3;
        List<Header> v4;
        try {
            Pair<String, String> v1_1 = b(arg9);
            String v0 = v1_1.first;
            String v2 = v1_1.second;
            ICommonApi commonApi = RetrofitUtils.createService(v0, ICommonApi.class);
            v4 = a(arg11);
            v3 = new LinkedHashMap();
            if (arg10 != null) {
                Iterator v5 = arg10.iterator();
                while (v5.hasNext()) {
                    Object v1_2 = v5.next();
                    ((Map) v3).put(((Pair) v1_2).first, ((Pair) v1_2).second);
                }
            }

            if (arg12 != null) {
                v5_1 = arg12.a;
            } else {
                v5_1 = false;
            }

            return commonApi.postForm(-1, v2, v3, v4, v5_1).execute().body;
        } catch (Exception v1) {
            if ((v1 instanceof HttpResponseException)) {
                throw new CommonHttpException(((HttpResponseException) v1).getStatusCode(), v1.getMessage());
            } else {
                throw new CommonHttpException(0, v1.getMessage());
            }
        }
    }

    public final String a(String arg6, Map arg7, com.bytedance.common.utility.e.a arg8) throws
            Exception {
        Pair<String, String> v1 = b(arg6);
        String v0 = v1.first;
        String url = v1.second;
        ICommonApi commonApi = RetrofitUtils.createService(v0, ICommonApi.class);
        List<Header> v3 = a(arg7);
        boolean v2 = arg8 != null ? arg8.a : false;
        return commonApi.get(-1, url, v3, v2).execute().body;
    }

    public final String a(String arg8, byte[] arg9, Map arg10, com.bytedance.common.utility.e.a arg11) throws CommonHttpException {
        boolean v5;
        List<Header> v4;
        Object v1_3 = null;
        ICommonApi commonApi;
        try {
            Pair<String, String> v1_1 = b(arg8);
            String v0 = v1_1.first;
            String v2 = v1_1.second;
            commonApi = RetrofitUtils.createService(v0, ICommonApi.class);
            String v1_2 = null;
            if (arg10 != null) {
                v1_3 = arg10.get("Content-Type");
                arg10.remove("Content-Type");
            }

            v4 = a(arg10);
            if (arg11 != null) {
                v5 = arg11.a;
            } else {
                v5 = false;
            }
            return commonApi.postData(-1, v2, new TypedByteArray(((String) v1_3), arg9), v4, v5).execute().body;
        } catch (Exception v1) {
            v1.printStackTrace();
            if ((v1 instanceof HttpResponseException)) {
                throw new CommonHttpException(((HttpResponseException) v1).getStatusCode(), v1.getMessage());
            }

            throw new CommonHttpException(0, v1.getMessage());
        }
    }
}

